﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace YSC_Core.BLL.Domain.Utils
{
    public static class UtilsHelp
    {
        /// <summary>
        /// Sets the cert policy.
        /// </summary>
        public static void SetCertificatePolicy()
        {
            ServicePointManager.ServerCertificateValidationCallback
                       += RemoteCertificateValidate;
        }

        /// <summary>
        /// Remotes the certificate validate.
        /// </summary>
        private static bool RemoteCertificateValidate(
           object sender, X509Certificate cert,
            X509Chain chain, SslPolicyErrors error)
        {
            // trust any certificate!!!
            System.Console.WriteLine("Warning, trust any certificate");
            return true;
        }

        /// <summary>
        /// 获取日期
        /// </summary>
        /// <param name="date"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        public static string Condition(DateTime date, string type)
        {
            if (type == "Day")
            {
                return date.ToString("yyyy-M-d");
            }
            else if (type == "Month")
            {
                return date.ToString("yyyy-M");
            }
            else if (type == "Years")
            {
                return date.ToString("yyyy");
            }
            return date.Year.ToString();
        }

        /// <summary>
        /// 生成4位随机数字
        /// </summary>
        /// <returns></returns>
        public static int GetRandNumInt()
        {
            Random r = new Random();
            int i = r.Next(1000, 9999);
            return i;
        }


        /// <summary>
        /// 生成随机数
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static String getRandNum(int count)
        {
            int number;
            string checkCode = String.Empty;     //存放随机码的字符串 

            Random random = new Random();

            for (int i = 0; i < count; i++) //产生4位校验码 
            {
                number = random.Next();
                number = number % 36;
                if (number < 10)
                {
                    number += 48;    //数字0-9编码在48-57 
                }
                else
                {
                    number += 55;    //字母A-Z编码在65-90 
                }

                checkCode += ((char)number).ToString();
            }
            return checkCode;
        }

        /// <summary> 
        /// 获取时间戳 
        /// </summary> 
        /// <returns></returns> 
        public static string GetTimeStamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }

        /// <summary>
        /// 进行HASH编码
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string GetHash(this string input)
        {
            HashAlgorithm hashAlgorithm = new SHA256CryptoServiceProvider();

            byte[] byteValue = Encoding.UTF8.GetBytes(input);

            byte[] byteHash = hashAlgorithm.ComputeHash(byteValue);

            return Convert.ToBase64String(byteHash);
        }

        /// <summary>
        /// 获取MD5字符串
        /// </summary>
        /// <param name="source">加密源</param>
        /// <returns>加密结果</returns>
        public static string GetMD5(this string source)
        {
            using (MD5 md5Hash = MD5.Create())
            {
                string hash = GetMd5Hash(md5Hash, source);
                return hash;
            }
        }

        /// <summary>
        /// 获取MD5哈希串
        /// </summary>
        /// <param name="md5Hash">md5加密对象</param>
        /// <param name="source">加密源</param>
        /// <returns>加密结果</returns>
        private static string GetMd5Hash(MD5 md5Hash, string source)
        {
            byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(source));

            StringBuilder sBuilder = new StringBuilder();

            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            return sBuilder.ToString();
        }

        /// <summary>
        /// SHA1编码
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string SHA1_Hash(this string source)
        {
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            byte[] bytes_sha1_in = Encoding.Default.GetBytes(source);
            byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
            string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
            return str_sha1_out;
        }

        /// <summary>
        /// Base64编码
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string Base64Encoding(this string source)
        {
            var encode = string.Empty;
            byte[] bytes = Encoding.UTF8.GetBytes(source);
            try
            {
                encode = Convert.ToBase64String(bytes);
            }
            catch
            {
                encode = source;
            }
            return encode;
        }
    }
}
